package com.ciwong.epaper.modules.login;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.bean.UserAccountInfo;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 已登录用户列表适配器
 * Created by Administrator on 2017/2/23.
 */
public class LoginAccountAdapter extends BaseAdapter {

    /**
     * 已登录过账号集合
     */
    private List<UserAccountInfo> mLoginAccountInfos;
    private Context mContext;

    public void setPopListItemListener(PopListItemListener popListItemListener) {
        mPopListItemListener = popListItemListener;
    }

    private PopListItemListener mPopListItemListener;

    /**
     * 构造函数
     *
     * @param loginAccountInfos
     *            已登录过账号集合
     */
    public LoginAccountAdapter(Context context, List<UserAccountInfo> loginAccountInfos)
    {
        this.mLoginAccountInfos = loginAccountInfos;
        this.mContext=context;
    }

    @Override
    public int getCount()
    {
        return mLoginAccountInfos == null ? 0 : mLoginAccountInfos.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mLoginAccountInfos == null || mLoginAccountInfos.isEmpty()
                ? null : mLoginAccountInfos.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent)
    {
        View view;
        if (convertView == null)
        {
//            view = View.inflate(mContext,
//                    R.layout.adapter_login_account, null);
	        view = LayoutInflater.from(mContext).inflate(R.layout.adapter_login_account, parent, false);
        }
        else
        {
            view = convertView;
        }
        ImageView loginAvatar = ViewHolder.get(view, R.id.login_avatar);
        TextView loginAccount = ViewHolder.get(view, R.id.login_account);
        ImageView loginDelete = ViewHolder.get(view, R.id.login_delete);

        UserAccountInfo loginAccountInfo= (UserAccountInfo) getItem(position);

        String avatar = loginAccountInfo.getAvatar();
        avatar = TextUtils.isEmpty(avatar)
                ? "drawable://" + R.drawable.about_icon : avatar;
        ImageLoader.getInstance().displayImage(avatar, loginAvatar,
                BaseConstants.getLoginCircleBuilder());

        if(loginAccountInfo.getPhonelogin()){
            loginAccount.setText(loginAccountInfo.getPhoneNumber());
        }else {
            loginAccount.setText(String.valueOf(loginAccountInfo.getUserId()));
        }
        loginDelete.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mPopListItemListener.deleteAccount(mLoginAccountInfos,position);
            }
        });

        return view;
    }

    public void addAll(List<UserAccountInfo> loginAccountInfos)
    {
        if (this.mLoginAccountInfos != null)
        {
            this.mLoginAccountInfos.addAll(loginAccountInfos);
            notifyDataSetChanged();
        }
    }

    public void clear()
    {
        if (this.mLoginAccountInfos != null)
        {
            this.mLoginAccountInfos.clear();
            notifyDataSetChanged();
        }
    }

}
